package com.wingice.config.mybatis;

import cn.hutool.core.util.ObjectUtil;
import com.wingice.mybatis.plus.autofill.KnifeMetaObjectHandler;
import com.wingice.spring.security.common.utils.SecurityUtils;
import com.wingice.spring.security.model.KnifeUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author 胡昊
 * @description Mybaits自动填充配置类
 * @email huhao9277@gmail.com
 * @date 2020/12/18 14:31
 */
@Slf4j
@Component
public class MyMetaObjectHandler extends KnifeMetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", null);
        metaObject.setValue("updateTime", null);
        metaObject.setValue("createBy", null);
        metaObject.setValue("updateBy", null);
        this.strictInsertFill(metaObject, "createTime", Long.class, System.currentTimeMillis()); // 起始版本 3.3.0(推荐使用)
        //  获取用户信息
        KnifeUser knifeUser = SecurityUtils.getUser();
        if (ObjectUtil.isNotEmpty(knifeUser)) {
            this.strictInsertFill(metaObject, "createBy", String.class, knifeUser.getId());
        }
        updateFill(metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", null);
        metaObject.setValue("updateBy", null);
        this.strictUpdateFill(metaObject, "updateTime", Long.class, System.currentTimeMillis()); // 起始版本 3.3.0(推荐使用)
        //  获取用户信息
        KnifeUser knifeUser = SecurityUtils.getUser();
        if (ObjectUtil.isNotEmpty(knifeUser)) {
            this.strictUpdateFill(metaObject, "updateBy", String.class, knifeUser.getId());
        }
    }


}
